import request from './config/Request';
import SnowflakeId from "snowflake-id"

//用户登录接口
export const AuthLogin = (param) => postForm('/api/v1/guest/login', param);
//获取验证码接口
export const AuthImage = () => get('/api/v1/guest/image');

//权限菜单
export const UserMenu = () => get('/api/v1/auth/menu');

//config
export const ConfigList = (query) => postForm('/api/v1/base/config/list', query);
export const ConfigAdd = (query) => postForm('/api/v1/base/config/add', query);
export const ConfigEdit = (query) => postForm('/api/v1/base/config/edit', query);
export const ConfigDel = (query) => postForm('/api/v1/base/config/del', query);
//用户接口
export const UserList = (query) => postForm('/api/v1/user/list', query);
export const UserAdd = (param) => postForm('/api/v1/user/add', param);
export const UserDel = (param) => postForm('/api/v1/user/del', param);
export const UserEdit = (param) => postForm('/api/v1/user/edit', param);
export const UserLock = (param) => postForm('/api/v1/user/lock', param);
//角色接口
export const RoleList = (query) => postForm('/api/v1/role/list', query);
export const RoleAdd = (param) => postForm('/api/v1/role/add', param);
export const RoleDel = (param) => postForm('/api/v1/role/del', param);
export const RoleEdit = (param) => postForm('/api/v1/role/edit', param);
export const RoleAll = (param) => postForm('/api/v1/role/all', param);
export const RoleAuth = (param) => postForm('/api/v1/role/auth', param);
//资源接口
export const ResView = () => get('/api/v1/res/view');
export const ResList = (query) => postForm('/api/v1/res/list', query);
export const ResAdd = (param) => postForm('/api/v1/res/add', param);
export const ResDel = (param) => postForm('/api/v1/res/del', param);
export const ResEdit = (param) => postForm('/api/v1/res/edit', param);
export const ResLevel = (param) => postForm('/api/v1/res/level', param);
//Job
export const JobList = (query) => postForm('/api/v1/task/job/list', query);
export const JobAdd = (query) => postForm('/api/v1/task/job/add', query);
export const JobEdit = (query) => postForm('/api/v1/task/job/edit', query);
export const JobDel = (query) => postForm('/api/v1/task/job/del', query);
export const JobChange = (query) => postForm('/api/v1/task/job/change', query);
export const JobExecute = (query) => postForm('/api/v1/task/job/execute', query);


export const get = function (url) {
    if (url.indexOf("?") > 0) {
        url = url + "&requestId=" + requestId();
    } else {
        url = url + "?requestId=" + requestId();
    }
    return request.get(url)
}

export const postForm = function (url, query) {
    let formData = new FormData();
    formData.append("requestId", requestId());
    for (let key in query) {
        formData.append(key, query[key]);
    }
    return request.post(url, formData);
}

//上传
export const postUpload = function (url, formData) {
    formData.append("requestId", requestId());
    return request.post(url, formData);
}

//进度上传
export const postUploadProgress = function (url, formData, configProgress) {
    let config = {};
    config.timeout = 1000 * 60 * 30;
    config.onUploadProgress = configProgress.onUploadProgress;
    formData.append("requestId", requestId());
    return request.post(url, formData, config);
}

export const requestId = () => {
    const snow = new SnowflakeId();
    return snow.generate();
}
